﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.IO.IsolatedStorage;

using Microsoft.Phone.Controls;

using My_App.Model;

namespace My_App
{
    public partial class MainPage : PhoneApplicationPage
    {
        internal static IsolatedStorageSettings My_IsolaledStorage = IsolatedStorageSettings.ApplicationSettings;
       internal  const string  task_string="My_Task";
        public MainPage()
        {
            
            InitializeComponent();
            InitializeSettings();
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        private void InitializeSettings()
        {
            if (!My_IsolaledStorage.Contains(task_string))
            {
                My_IsolaledStorage.Add(task_string, null);
            }
           
        }
        private void MainListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MainListBox.SelectedIndex == -1)
                return;

            if (MainListBox.SelectedIndex >= 0)
            {
                App.ViewModel.Items[MainListBox.SelectedIndex].State = TaskState.Edit;
            }

            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

            MainListBox.SelectedIndex = -1;
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {

            if (My_IsolaledStorage.Contains(task_string)) { My_IsolaledStorage[task_string]= App.ViewModel.Items; My_IsolaledStorage.Save(); }
        }
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void AddClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/DetailsPage.xaml", UriKind.Relative));
        }

        private void DeleteClick(object sender, EventArgs e)
        {
            int index = 0;
            do
            {
                if (App.ViewModel.Items[index].IsSelected == true)
                {
                    App.ViewModel.Items.RemoveAt(index); index--;
                }

                index++;
            }
            while (index != App.ViewModel.Items.Count);
        }

        private void CompleteClick(object sender, EventArgs e)
        {
            foreach (var task in App.ViewModel.Items)
            {
                if (task.IsSelected)
                {
                    task.State = TaskState.Complited;
                }
                else
                {
                    task.State = TaskState.Normal;
                }
            }

            App.ViewModel.Items = new ObservableCollection<Task>(App.ViewModel.Items.OrderBy(i => i.State));
        }
    }
}